package cn.tedu.fresheveryday.controller;

import cn.tedu.fresheveryday.pojo.entity.ShopCart;
import cn.tedu.fresheveryday.pojo.entity.User;
import cn.tedu.fresheveryday.pojo.vo.ShopCartVO;
import cn.tedu.fresheveryday.restful.JsonResult;
import cn.tedu.fresheveryday.service.IShopCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Api(tags = "购物车模块")
@RequestMapping(path = "/shop_cart", produces = "application/json;charset=utf-8")
public class ShopCartController {
    @Autowired
    private IShopCartService service;

    /**
     * 添加商品到购物车
     * @param shopCart
     */
    @ApiOperation("添加购物车")
    @PostMapping("/insert")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "productId", name="productId", required = true),
            @ApiImplicitParam(value = "money", name="money", required = true),
            @ApiImplicitParam(value = "quantity", name="quantity", required = true)
    })
    public JsonResult insert(@RequestBody ShopCart shopCart, HttpSession session) {
        User user = (User) session.getAttribute("user");

        if (user != null) {
            // 设置id、用户id和添加时间
            shopCart.setUserId(user.getId());

            // 保存数据
            service.insert(shopCart);
            // 购物车信息储存到session中
            session.setAttribute("shopCart", shopCart);

            return JsonResult.success(null, shopCart.getId());
        }

        return JsonResult.error("请先登录");
    }

    /**
     * 通过id删除购物车
     * @param id
     * @return
     */
    @GetMapping("/deleteById/{id}")
    @ApiOperation("通过id删除购物车")
    @ApiImplicitParam(value = "id", name="id", required = true)
    public JsonResult<Void> deleteById(@PathVariable("id") String id) {
        service.deleteById(id);

        return JsonResult.success("删除成功");
    }

    /**
     * 通过id查询购物车信息
     * @param id
     * @return
     */
    @GetMapping("/selectById")
    @ApiOperation("通过id查询购物车")
    @ApiImplicitParam(value = "id", name="id", required = true)
    public ShopCartVO selectById(String id) {
        return service.selectById(id);
    }

    /**
     * 查询用户的购物车
     * @param session
     * @return
     */
    @GetMapping("/select")
    @ApiOperation("查询用户的购物车")
    public List<ShopCartVO> select(HttpSession session) {
        User user = (User) session.getAttribute("user");

        if (user != null)
            return service.selectByUserId(user.getId());
        return null;
    }

    /**
     * 查询购物车数量
     * @param session
     * @return
     */
    @ApiOperation("查询购物车数量")
    @GetMapping("/selectCount")
    public int selectCount(HttpSession session) {
        User user = (User) session.getAttribute("user");

        if (user != null)
            return service.selectCount(user.getId());
        return 0;
    }

}